#pragma once

#include "JobQueue.h"
#include <mysql/mysql.h>

struct threadpool;  // 类型声明

typedef struct thread {
    int id;         // 对人类友好的id
    pthread_t tid;  // 线程id
    MYSQL* mysql;   // MySQL 连接
    struct threadpool* tpool;   // 指向所属线程池    
} Thread;

typedef struct threadpool {
    Thread* threads;  
    JobQueue jobs;       // 任务队列
    volatile int threads_alive; // 每次从内存中读取变量的值，而不会使用寄存器中值
    volatile int threads_working;
    volatile int keep_alive;    // 线程池中的线程是否继续存活
    pthread_mutex_t count_lock;
    pthread_cond_t  all_idle;   // 指示：是不是所有线程都是空闲
} ThreadPool;

void threadpool_init(ThreadPool* tpool, int n);
void threadpool_add_task(ThreadPool* tpool, void (*job)(void*, Thread*), void* arg);
// 等待所有线程都处于空闲状态
void threadpool_wait(ThreadPool* tpool);
void threadpool_destroy(ThreadPool* tpool);
